<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\zjs\controller;
use app\admin\model\Customer;
use app\admin\model\Employee;
use GuzzleHttp\Client;
use think\Db;
/**
 * Description of Public
 *
 * @author HuangLuSen
 */
class Publics extends Zjs {
    public function bangding() {
        return $this->fetch('index/personal_binding');
    }
    public function validates() {
        if($this->request->isAjax()){
           $data = $this->request->post();
            if(empty($data['phone'])){
                $this->error('号码不能为空！');die;
            }
            $userdata = Customer::get(['mobile'=>$data['phone']]);
            if(!empty($userdata)){
                $this->error('该号码已经被绑定！');die;
            }
            if($data['attr']==154){//当用户为员工到时候
                $list = Employee::get(['phone'=>$data['phone']]);
                if(!empty($list)){
                    $list=$list->toArray();
                    $list['Status']=TRUE;
                    $list['Info']['CustomerName']=$list['name'];
                }else{
                    $list['Status']=false;
                }
            }else{
                $url = "http://222.180.151.118:8086/Introduce.asmx/introduceMain";
                $client = new Client();
                $datas = $client->request('POST', $url, [
                    'form_params' => [
                        'phone' => $data['phone'],
                        'keyword' => 'MRS'
                    ]
                ]);
                $lists = (string)$datas->getBody();
                $dom = new \DOMDocument();
                $dom->loadXML($lists);
                $arr = $dom->documentElement->textContent;
                $arr = json_decode($arr, true);
                if (!empty($arr)) {
                    $list['Status'] = true;
                    $list['Info']['CustomerName'] = $arr[0]['CusMomName'];
                } else {
                    $list['Status'] = false;
                }
            }
            if($list['Status']){
                $user = new Customer();
                $user->mobile=$data['phone'];
                $user->username=$list['Info']['CustomerName'];
                $user->attr=$data['attr'];
                $user->avatar= session('user_zjs.avatar');
                $user->sex= session('user_zjs.sex');
                $user->country= session('user_zjs.country');
                $user->province= session('user_zjs.province');
                $user->city= session('user_zjs.city');
                $user->openid= session('user_zjs.openid');
                $user->nickname= $this->filterEmoji(session('user_zjs.nickname'));
                $user->status= 1;
                $user->bumen= $list['sid'];
                $user->save();
                if($user->id>0){
                    session('user_id', $user->id);
                    session('user_attr', $user->attr);
                    $this->success('恭喜您，绑定成功！', url('tj/index'));
                }else{
                    $this->error('绑定失败，请联系客服');
                }
            }else{
                 $this->error('没有找到该号码的信息，请联系客服！');die;
            }
        }else{
            $this->error('请求的方式错误！');
        }
    }
    public function filterEmoji($str)
    {
        $str = preg_replace_callback(
            '/./u',
            function (array $match) {
                return strlen($match[0]) >= 4 ? '' : $match[0];
            },
            $str);

        return $str;
    }
}
